<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" stripe>
          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column prop="name" :label="i18n('contact.name')">
          </el-table-column>

          <el-table-column prop="mobile" :label="i18n('contact.mobile')" width="120">
          </el-table-column>

          <el-table-column :label="$t('contact.sex')" width="80">
            <template v-slot="scope">
              {{ scope.row.sex.text }}
            </template>
          </el-table-column>

          <el-table-column prop="position" :label="i18n('contact.position')" width="140">
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('contact.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:contact:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_contact_form', query: { id: scope.row.id, client_id: dataForm.client_id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:contact:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="client-contact-index">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/contact')

  const dataForm = reactive({
    client_id: '',
  })

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    dataForm.client_id = query.id

    getDataList(model)
  })
</script>
